package models

import (
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type InspectsPriceLog struct {
	Id        int64
	Inspects  *Inspects `orm:"rel(fk);null"`
	OldPrice  float64
	NowPrice  float64
	CreatedBy string
	CreatedAt int64
}

func (a *InspectsPriceLog) TableName() string {
	return InspectsPriceLogTableName()
}

func InspectsPriceLogTableName() string {
	return TableName("sa_inspects_price_log")
}

//查询
type InspectsPriceLogQueryParam struct {
	BaseQueryParam
	InspectsId int64
}

//查询分页数据
func InspectsPriceLogPageList(params *InspectsPriceLogQueryParam) ([]*InspectsPriceLog, int64) {
	query := orm.NewOrm().QueryTable(InspectsPriceLogTableName())
	data := make([]*InspectsPriceLog, 0)
	sortorder := "-Id"

	switch params.Sort {
	case "Id":
		sortorder = "Id"
	}
	if params.Order == "desc" {
		sortorder = "-" + sortorder
	}
	query = query.Filter("inspects_id", params.InspectsId)
	total, _ := query.Count()

	query.OrderBy(sortorder).Limit(params.Limit, params.Offset).All(&data)

	return data, total
}

func InspectsPriceLogAdd(m *InspectsPriceLog) error {
	m.CreatedAt = time.Now().Unix()
	if _,err := orm.NewOrm().Insert(m);err != nil {
		return err
	}
	return nil
}